﻿using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Zenject;

public class RoleSelectUIManager : MonoBehaviourSingleton<RoleSelectUIManager>
{
    [Inject] private RoleService _roleService;
    [Inject] private DiContainer _container;

    [Header("角色选择GridUI")] [SerializeField]
    private GameObject parent;

    [SerializeField] private GameObject roleSelectUIPrefab;
    private List<RoleSelectUI> _roleSelectUIList = new List<RoleSelectUI>();

    [Header("角色展示UI")] [SerializeField] private Image avatarImage;
    [SerializeField] private TextMeshProUGUI roleNameText;
    [SerializeField] private TextMeshProUGUI roleDescribeText;
    [SerializeField] private TextMeshProUGUI greatRecordText;


    [Header("武器选择")] [SerializeField] private GameObject weaponSelectUI;


    /**
     * 初始化角色选择的GridUI
     */
    private void Start()
    {
        foreach (Role role in _roleService.SelectAllHero())
        {
            GameObject roleUI = _container.InstantiatePrefab(roleSelectUIPrefab, parent.transform);
            roleUI.GetComponent<RoleSelectUI>()?.InitUI(role);
            _roleSelectUIList.Add(roleUI.GetComponent<RoleSelectUI>());
        }

        _roleSelectUIList[0].UpdateRoleInfoUI();
    }

    /**
     * 切换武器选择UI
     */
    public void SwitchWeaponSelectUI()
    {
        //切换武器选择UI
        SwitchUIManager.Instance.SwitchUI(weaponSelectUI);
    }

    /**
    * 鼠标移入GridUI单元格时，更新角色展示UI
    */
    public void UpdateRoleInfoUI(Role role)
    {
        if (role.unlock)
        {
            //解锁
            roleNameText.text = role.name;
            roleDescribeText.text = role.describe.Replace("\\n", "\n");
            avatarImage.sprite = Resources.Load<Sprite>(role.avatar);
        }
        else
        {
            //未解锁
            roleNameText.text = "？？？";
            roleDescribeText.text = role.unlockConditions;
            avatarImage.sprite = Resources.Load<Sprite>("Image/UI/锁");
        }

        if (role.record >= 0)
        {
            greatRecordText.text = $"最大通过难度系统:{role.record.ToString()}";
        }
        else
        {
            greatRecordText.text = "尚无记录";
        }
    }

    /**
     * 随机选择一个角色
     */
    public void RandomRole()
    {
        List<RoleSelectUI> unlockRoleSelectUIs = _roleSelectUIList.Where(i => i._role.unlock).ToList();
        //随机选择一个
        unlockRoleSelectUIs[Random.Range(0, unlockRoleSelectUIs.Count)].SelectRole();
    }
}